<?php
Class UserlevelAction extends Action{
	public function user(){
		$user['id'] =$this->userid = intval(I('id'));
		$this->user = $user = M('User')->where($user)->find();//当前用户的一些基本信息
		$this->level = M('UserLevel')->select();
		$price = M('UserLevel')->where("id={$user['level']}")->find();
		$this->recordprice = $price['recordprice'];
		$this->discussprice = $price['discussprice'];
		$this->subprice = $price['subprice'];
		$this->packageprice = $user['packageprice'];


		$this->count = M('MedicalRecords')->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->count();
		// import('ORG.Util.EditPage');
		// $this->count = $count = M('MedicalRecords')->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->count();
		// $Page = new Page($count,10);
  //       $Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		// $this->page = $Page->show();
  //       $this->records =  M('MedicalRecords')->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();	

		if(!$user['packageend']){
			$pubtime = M("MedicalRecords")->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->order("pubtime desc")->getField("pubtime");
			$this->packageend=date("Y-m-d",$pubtime);
			$this->countlimit =  M("MedicalRecords")->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2 AND pubtime<='{$pubtime}'")->count();
		}else{
			$this->packageend=$user['packageend'];
			$pubtime = strtotime($user['packageend']);
			$this->countlimit =  M("MedicalRecords")->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2 AND pubtime<='{$pubtime}'")->count();
		}        
		$this->sampleprice = $this->countlimit * $this->recordprice;
		$this->display();
	}

	public function getprice(){
		$condition['id']=I('level');
		$price = M('UserLevel')->where($condition)->find();
		$this->ajaxReturn($price,"JSON");
	}

	public function getpackageend(){
		$pubtime=I('packageend');
		$userid = I('userid');
		$count = M("MedicalRecords")->where("user_id={$userid} AND pubtype=2 AND cashtype=2 AND pubtime<='{$pubtime}'")->count();
		$this->ajaxReturn(array("countlimit"=>$count),"JSON");
	}

	public function changelevel(){
		$condition['id']=intval(I('userid'));
		$data['level']=intval(I('level'));
		$save = M('User')->where($condition)->save($data);
		//echo M('User')->getLastSql();
		if($save!==false){
			$this->ajaxReturn(array("status"=>1),"JSON");
		}else{
			$this->ajaxReturn(array("status"=>0),"JSON");
		}
	}

	public function changepackage(){
		$condition['id']=intval(I('userid'));
		$data['packageend']=I('packageend');
		$data['packageprice']=intval(I('packageprice'));
		$save = M('User')->where($condition)->save($data);
		//echo M('User')->getLastSql();
		if($save!==false){
			$this->ajaxReturn(array("status"=>1),"JSON");
		}else{
			$this->ajaxReturn(array("status"=>0),"JSON");
		}
	}

	public function listpage(){
		$user['id'] = intval(I('id'));
		import('ORG.Util.InlinePage');
		$count = M('MedicalRecords')->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->count();
		$Page = new Page($count,10);
        $Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		$this->page = $Page->show();
        $this->records =  M('MedicalRecords')->where("user_id={$user['id']} AND pubtype=2 AND cashtype=2")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();	
        $this->display();	
	}
}